package com.sunelec.controller;

/**
 * @Author: 郑明利
 * @Description:
 * @Date: Create in 10:31 2018/06/03
 * @Modified By:
 */
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sunelec.pojo.Users;
import org.springframework.ui.Model;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/*
 * 自定义拦截器，针对所有url（除login和静态资源）
 */
public class MyHandlerInterceptor implements HandlerInterceptor {

    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {

    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
            throws Exception {


    }

    //解决用户不登录就进入到内面的网页
    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
        HttpSession session = arg0.getSession();
        if(session.getAttribute("users")==null){
            arg0.getRequestDispatcher("/login").forward(arg0,arg1);//重定向
            arg0.setAttribute("msg","请先登陆！");
            return false;
        }
        return true;
    }

}
